package com.ihelp.rest.mis.platform.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ihelp.rest.mis.platform.service.AdmdivisionService;
import com.ihelp.rest.mis.pojo.Admdivision;
import com.ihelp.rest.utils.HtmlUtil;

@Controller
@RequestMapping("/system/admdiv")
public class AdmdivisionController {
	
	@Autowired
	private AdmdivisionService admdivisionService;
	
	/**
	 * 行政区划管理主页面
	 * @param request
	 * @return
	 */
	@RequestMapping("admdivList.do")
	public String admdivList(HttpServletRequest request){
		return "system/admdiv/admdivList";
	}
	
	/**
	 * 行政区划新增和修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping("admdivAdd.do")
	public String admdivAdd(HttpServletRequest request,String parentcode,String id){
		Map<String, String> ref = new HashMap<String, String>();
		if(StringUtils.isNotBlank(id)){
			Admdivision admdivision = admdivisionService.getAdmdivById(id);	
			ref.put("saveType", "modify");
			request.setAttribute("admdivision", admdivision);
			Admdivision parentAdmdiv = new Admdivision();
			if(admdivision.getParentcode().equals("0")){
				parentAdmdiv.setAdcode("0");
				parentAdmdiv.setAdname("无上级");
			}else{
				parentAdmdiv = admdivisionService.getAdmdivByAdcode(admdivision.getParentcode());
			}
			request.setAttribute("parentAdmdiv", parentAdmdiv);
		}else{		
			Admdivision parentAdmdiv = new Admdivision();
			if(parentcode.equals("0")){
				parentAdmdiv.setAdcode("0");
				parentAdmdiv.setAdname("无上级");
				ref.put("newcode", "");
			}else{
				parentAdmdiv = admdivisionService.getAdmdivByAdcode(parentcode);
				ref.put("newcode", parentcode);
			} 
			request.setAttribute("parentAdmdiv", parentAdmdiv);
		}
		request.setAttribute("ref", ref);
		return "system/admdiv/admdivAdd";
	}
	
	/**
	 * 得到行政区划树
	 * @param response
	 * @param parentcode
	 */
	@RequestMapping("doGetAdmdiv.do")
	public void doGetAdmdiv(HttpServletResponse response, String parentcode){
		String json = this.admdivisionService.doGetAdmdiv(parentcode);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 行政区划列表
	 * @param response
	 * @param queryCondition
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 */
	@RequestMapping("jsonAdmdivPageList.do")
	public void jsonAdmdivPageList(HttpServletResponse response, Admdivision queryCondition,int page,int rows,String sort, String order){
		String json = this.admdivisionService.findAdmdivsPage(queryCondition, page, rows, sort, order);
		HtmlUtil.writerJson(response, json);
	}
	
	/**
	 * 保存行政区划
	 * @param response
	 * @param admdiv
	 */
	@RequestMapping("doSaveAdmdiv.do")
	public void doSaveAdmdiv(HttpServletResponse response, Admdivision admdiv){
		String result = this.admdivisionService.saveAdmdivision(admdiv);
		HtmlUtil.sendDirectToClient(response,result);
	}
	
	/**
	 * 删除行政区划
	 * @param response
	 * @param id
	 */
	@RequestMapping("doDelAdmdiv.do")
	public void doDelAdmdiv(HttpServletResponse response, String id){
		String result = this.admdivisionService.delAdmdivision(id);
		HtmlUtil.sendDirectToClient(response,result);
	}
}
